#include "helper.h"
#include "monitor.h"
#include "reg.h"

extern uint32_t instr;
extern char assembly[80];

/* decode J-type instrucion with unsigned immediate */
static void decode_jump_type(uint32_t instr) {

	//target
	op_src1->type = OP_TYPE_JUMP;
	op_src1->simm = (instr & INDEX_MASK);
	op_src1->val = op_src1->simm;

}

make_helper(j) {

	decode_jump_type(instr);
	int32_t pc_target = cpu.pc & 0xf0000000;
	int32_t instr_index = (op_src1->simm << 2);
	int32_t target = pc_target | instr_index;
	cpu.pc = target - 0x00000004;
	sprintf(assembly, "j   0x%08x", target);
}

make_helper(jal) {

	decode_jump_type(instr);
	int32_t pc_target = cpu.pc & 0xf0000000;
	int32_t instr_index = (op_src1->simm << 2);
	int32_t target = pc_target | instr_index;
	reg_w(31) = cpu.pc + 8;
	cpu.pc = target - 0x00000004;
	sprintf(assembly, "jal   0x%08x", target);
}

